<!DOCTYPE html>
<html>

<head>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>基于大数据的保护运行分析支撑系统</title>

<!-- CSS -->
<link rel="stylesheet" href="resources/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="resources/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="resources/css/form-elements.css">
<link rel="stylesheet" href="resources/css/style.css">
<link rel="apple-touch-icon-precomposed" sizes="144x144"
	href="resources/ico/apple-touch-icon-144-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="114x114"
	href="resources/ico/apple-touch-icon-114-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="72x72"
	href="resources/ico/apple-touch-icon-72-precomposed.png">
<link rel="apple-touch-icon-precomposed" href="resources/ico/apple-touch-icon-57-precomposed.png">

</head>

<body>
	<!-- Top content -->
	<div class="top-content">

		<div class="inner-bg">
			<div class="container">
				<div class="row">
					<div class="col-sm-8 col-sm-offset-2 text">
						<h1>
							<strong id="systemName">基于大数据的保护运行分析支撑系统</strong>
						</h1>
					</div>
				</div>
				<div class="row">
					<div class="col-sm-6 col-sm-offset-3 form-box">
						<div class="form-top">
							<div class="form-top-left">
								<h3 id="systemLogin">系统登录</h3>
								<p id="inputTip">请输入工号与密码:</p>
							</div>
							<div class="form-top-right">
								<i class="fa fa-key"></i>
							</div>
						</div>
						<div class="form-bottom">
							<form role="form" method="post" class="login-form">
								<div class="form-group">
									<label id="jobNumber" class="sr-only" for="form-userName">用户名</label> <input type="text"
										name="form-userName" value="" placeholder="用户名..." class="form-userName form-control"
										id="form-userName">
									<input type="hidden" id="form-username2">
								</div>
								<div class="form-group">
									<label id="password" class="sr-only" for="form-password">密码</label> <input type="password"
										name="form-password" value="" placeholder="密码..." class="form-password form-control"
										id="form-password">
								</div>
								<div class="form-group">
									<input type="text" class="form-control" placeholder="验证码..." id="safeCode" name="safeCode" style="width: 60%;">
									<div style="float: right; width: 116px; height: 46px; margin-top: -44px;">
										<img id="codeImg" alt="点击更换" title="点击更换" onclick="changeSafeCode()" src="../Kaptcha"/>
									</div>
								</div>
								<div class="form-group">
									<div class="form-group">
										<label id="remember" style="margin: 5px; font-weight: normal;"> <input
											id="rememberUser" style="margin: 5px;" type="checkbox"> 记住我
										</label> 
										<!-- <label id="auto" style="font-weight: normal; margin: 5px;"> <input id="autoLogin"
											style="margin: 5px;" type="checkbox"> 自动登录 -->
										<label id="auto" style="font-weight: normal; margin: 5px;"> <input id="autoLogin"
											style="margin: 5px;" type="hidden">
										</label>
										<label style="font-weight: normal; margin: 5px;" class="pull-right"> <input
											id="locale" style="margin: 5px;" type="checkbox" onclick="changeLanguage()" /> English
										</label>
									</div>
								</div>
								<button type="button" class="btn" id="loginBtn">登录</button>
								<input type="hidden" value="false" name="logOff"> <input type="hidden"
									value="Afterwork" name="globaltheme">
							</form>
						</div>
					</div>
				</div>
			</div>
		</div>

	</div>


	<!-- Javascript -->
	<script src="/resources/js/jquery.js"></script>
	<script src="/resources/bootstrap/js/bootstrap.min.js"></script>
	<script src="/resources/plugins/jQueryUI/jquery.backstretch.min.js"></script>
	<script src="/resources/plugins/base64/base64.js"></script>
	<script src="/resources/plugins/md5/md5.js"></script>
	<script src="/resources/plugins/crypto-js/aes.min.js"></script>

	<!--[if lt IE 10]>
            <script src="resources/js/placeholder.js"></script>
        <![endif]-->
	<script type="text/javascript">
		var titlesEN = {
			systemName : 'PROTECTION OPERATION ANALYSIS SUPPORT SYSTEM BASED ON BIG DATA',
			systemLogin : 'Login',
			enterTip : 'please enter the username and password:',
			jobNumber : 'jobNumber:',
			jobNumberInput : 'username...',
			password : 'password:',
			passwordInput : 'password...',
			rememberMe : 'Remember Me',
			autoLoginTip : 'Auto Login',
			loginBtn : 'SIGN IN',
			jobNumberIsNull : 'the username is empty',
			passwordIsNull : 'the password is empty ',
			safeCodeIsNull : 'the safecode is empty '
		}
		var rememberUser = "false"; //记住密码的cookie状态
		var autoLogin = "false"; //自动登录的cookie状态
		var titlesZH = {
			systemName : '基于大数据的保护运行分析支撑系统',
			systemLogin : '系统登录',
			enterTip : '请输入工号与密码',
			jobNumber : '用户名：',
			jobNumberInput : '用户名...',
			password : '密码：',
			passwordInput : '密码...',
			rememberMe : '记住我',
			autoLoginTip : '自动登录',
			loginBtn : '登录',
			jobNumberIsNull : '工号为空',
			passwordIsNull : '密码为空',
			safeCodeIsNull : '验证码为空'
		}
		function englishErrorMessage(data) {
			if (document.getElementById('locale').checked) {
				if (data.indexOf('密码输入错误，您还有') >= 0) {
					var num = data.substring(10, 11);
					data = "The password is wrong, and you have " + num + "  chances";
				}
				if (data.indexOf('不存在此账户信息') >= 0) {
					data = "There is no information about this username";
				}
				if (data.indexOf('分钟后尝试') >= 0) {
					var num = data.substring(3, 5);
					data = "Please try it again after " + num + " minutes";
				}
				if (data.indexOf('密码输入次数过多，该账户被锁定，请您30分钟后进行尝试') >= 0) {
					data = "The number of error passwords you entered is too many times, the account is locked. Please try it after 30 minutes";
				}
				if (data.indexOf('密码输入错误，此账号将继续锁定') >= 0) {
					data = "Password input error, this account will continue to lock";
				}
			}
			return data;
		}
		jQuery(document).ready(
				function() {
					/*
					    Fullscreen background
					 */
					$.backstretch("/resources/img/backgrounds/1.jpg");
					/*
					    Form validation
					 */
					$('.login-form input[type="text"], .login-form input[type="password"], .login-form textarea').on(
							'focus', function() {
								$(this).removeClass('input-error');
							});
					document.onkeydown = function(e) {
						if (e.keyCode == 13) {
							login(e);
						}
					}
					checkCookie();
					$('#loginBtn').on('click', login);
					function getCookie(c_name) {
						if (document.cookie.length > 0) {
							var b = new Base64();
							var coo = document.cookie;
							var c_start = coo.indexOf(c_name + "=");
							if (c_start != -1) {
								c_start = c_start + c_name.length + 1;
								var c_end = document.cookie.indexOf(";", c_start);
								if (c_end == -1) {
									c_end = document.cookie.length;
								}
								return b.decode(unescape(document.cookie.substring(c_start, c_end)));
							}
						}
						return ""
					}

					function setCookie(obj) {
						var b = new Base64();
						var date = new Date();
						var expiresDays = 365;
						date.setTime(date.getTime() + expiresDays * 24 * 3600 * 1000);
						for ( var i in obj) {
							document.cookie = i + '=' + escape(b.encode(obj[i])) + ';expires=' + date.toGMTString();
						}
					}

					function checkCookie() {
						var b = new Base64();
						var userName = getCookie('userName');
						var password = getCookie('password');
						rememberUser = getCookie('rememberUser');
						autoLogin = getCookie('autoLogin');
						var locale = getCookie('locale');
						$("#form-userName").val(userName);
						$("#form-password").val(password);
						if (rememberUser == 'true') {
							document.getElementById('rememberUser').checked = true;
						}
						if (locale == 'true') {
							document.getElementById('locale').checked = true;
							changeEn();
						} else {
							changeZH();
						}
						if (autoLogin == 'true') {
							document.getElementById('autoLogin').checked = true;
							login();
						}

					}
					function clearCookie(arr) {
						for ( var i in arr) {
							var exp = new Date();
							exp.setTime(exp.getTime() - 1);
							var cval = getCookie(arr[i]);
							if (cval != null)
								document.cookie = arr[i] + "=" + cval + ";expires=" + exp.toGMTString();
						}
					}
					function addLoginTimeByCode(userName) {
						$.getJSON('/rest/login/addLoginTimeByCode/' + JSON.stringify(userName), function() {
						});
					}
					function login(e) {
						$("#error").remove();
						userName = $("#form-userName").val().trim();
						password = $("#form-password").val().trim();
						var logOff = $("input[name='logOff']").val().trim();
						var locale = document.getElementById('locale').checked;
						var safeCode = $("#safeCode").val().trim();
						if (userName == "") {
							var tips = "";
							if (document.getElementById('locale').checked) {
								tips = titlesEN.jobNumberIsNull;
							} else {
								tips = titlesZH.jobNumberIsNull;
							}
							var span = $("<span id='error' style='color:red'>" + tips + "</span>").appendTo(
									$(".form-top-left p"));
						} else if (password == "") {
							var tips = "";
							if (document.getElementById('locale').checked) {
								tips = titlesEN.passwordIsNull;
							} else {
								tips = titlesZH.passwordIsNull;
							}
							var span = $("<span id='error' style='color:red'>" + tips + "</span>").appendTo(
									$(".form-top-left p"));
						} else if (safeCode == "") {
							var tips = "";
							if (document.getElementById('locale').checked) {
								tips = titlesEN.safeCodeIsNull;
							} else {
								tips = titlesZH.safeCodeIsNull;
							}
							var span = $("<span id='error' style='color:red'>" + tips + "</span>").appendTo(
									$(".form-top-left p"));
						} else if (userName != "" && password != "" && safeCode != "") {
							var key = CryptoJS.enc.Utf8.parse("abcdefgabcdefg12");
							var userNameAes = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(userName), key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
							$("#form-username2").val(userNameAes);
							$.ajax({
								async : false,
								url : "/loginCheck",
								type : "post",
								data : {
									"form-username" : $("#form-username2").val(),
									"form-password" : md5(password),
									"locale" : locale,
									"logOff" : logOff,
									"safeCode" : safeCode
								},
								success : function(data) {
									switch (data) {
									case "success":
									var autoLogin = document.getElementById('autoLogin').checked;
									var rememberUser = document.getElementById('rememberUser').checked;
									if (autoLogin || rememberUser) {
										setCookie({
											'userName' : escape(userName),
											'password' : escape(password),
											'rememberUser' : escape(rememberUser + ''),
											autoLogin : escape(autoLogin + ''),
											'locale' : locale + ''
										});
									} else {
										clearCookie([ 'userName', 'password', 'rememberUser', 'autoLogin', 'locale' ]);
									}
									location.href = "/index.xhtml?d=" + new Date().getTime();
									addLoginTimeByCode(userName);
										break;
									default:
										data = englishErrorMessage(data);
										var span = $("<span id='error' style='color:red'>" + data + "</span>").appendTo(
												$(".form-top-left p"));
										$("#safeCode").val("");
										changeSafeCode();//刷新验证码
										break;
									}
								}
							});
						}
					}
				})

		function changeEn(remember, auto) {
			document.getElementById('systemName').innerHTML = titlesEN.systemName;
			document.getElementById('systemLogin').innerHTML = titlesEN.systemLogin;
			document.getElementById('inputTip').innerHTML = titlesEN.enterTip;
			document.getElementById('jobNumber').innerHTML = titlesEN.jobNumber;
			document.getElementById('form-userName').innerHTML = titlesEN.jobNumberInput;
			document.getElementById('form-userName').placeholder = titlesEN.jobNumberInput;
			document.getElementById('password').innerHTML = titlesEN.password;
			document.getElementById('form-password').innerHTML = titlesEN.passwordInput;
			document.getElementById('form-password').placeholder = titlesEN.passwordInput;
			document.getElementById('loginBtn').innerHTML = titlesEN.loginBtn;
			document.getElementById('remember').innerHTML = document.getElementById('remember').innerHTML.replace(
					"记住我", titlesEN.rememberMe);
			document.getElementById('auto').innerHTML = document.getElementById('auto').innerHTML.replace("自动登录",
					titlesEN.autoLoginTip);
			if (rememberUser == "true") {
				document.getElementById('rememberUser').checked = true;
			}
			if (autoLogin == "true") {
				document.getElementById('autoLogin').checked = true;
			}
		}
		function changeZH(remember, auto) {
			document.getElementById('systemName').innerHTML = titlesZH.systemName;
			document.getElementById('systemLogin').innerHTML = titlesZH.systemLogin;
			document.getElementById('inputTip').innerHTML = titlesZH.enterTip;
			document.getElementById('jobNumber').innerHTML = titlesZH.jobNumber;
			document.getElementById('form-userName').innerHTML = titlesZH.jobNumberInput;
			document.getElementById('form-userName').placeholder = titlesZH.jobNumberInput;
			document.getElementById('password').innerHTML = titlesZH.password;
			document.getElementById('form-password').innerHTML = titlesZH.passwordInput;
			document.getElementById('form-password').placeholder = titlesZH.passwordInput;
			document.getElementById('loginBtn').innerHTML = titlesZH.loginBtn;
			document.getElementById('remember').innerHTML = document.getElementById('remember').innerHTML.replace(
					"Remember Me", titlesZH.rememberMe);
			document.getElementById('auto').innerHTML = document.getElementById('auto').innerHTML.replace("Auto Login",
					titlesZH.autoLoginTip);
			if (rememberUser == "true") {
				document.getElementById('rememberUser').checked = true;
			}
			if (autoLogin == "true") {
				document.getElementById('autoLogin').checked = true;
			}
		}
		function changeLanguage() {
			var locale = document.getElementById('locale');
			if (document.getElementById('rememberUser').checked) {
				rememberUser = "true";
			}
			if (document.getElementById('autoLogin').checked) {
				autoLogin = "true";
			}
			if (locale.checked == true) {
				changeEn();
			} else {
				changeZH();
			}
		}
		
		//刷新验证码
		function changeSafeCode(){
			var codeImg = document.getElementById("codeImg");
			codeImg.src = "../Kaptcha?"+Math.random();
		}
	</script>
</body>
</html>
